﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AutoGear.BLL;
using ECommerce.Library;
using System.IO;
using ECommerce.DAL;
using Entity;
using eShop.Model;
using eShop.Business;

public partial class admin_manufacturer_edit : SiteAdminBasePage
{
    private Int32 BrandId
    {
        get
        {
            try
            {
                if (HttpContext.Current.Request.QueryString != null && !String.IsNullOrEmpty(HttpContext.Current.Request.QueryString["id"]))
                {
                    return Convert.ToInt32(HttpContext.Current.Request.QueryString["id"]);
                }
                else
                {
                    return -1;
                }
            }
            catch
            {
                return -1;
            }
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (BrandId != -1)
            {
                LoadBrandData();
            }
        }
    }

    private void LoadBrandData()
    {
        Manufacturer brand = ManufacturerManager.GetById(BrandId);
        if (brand != null)
        {
            tbxBrandName.Text = brand.BrandName;
            imgDefaultImagePreview.ImageUrl = ProductFileAccessUtility._brandLogoPath + brand.BrandImageUrl;
            imgDefaultBannerPreview.ImageUrl = ProductFileAccessUtility._brandBannerPath + brand.BrandBannerImageUrl;
            cbxIsActive.Checked = brand.IsActive;
        }
    }

    protected void Create_Click(object sender, EventArgs e)
    {
        try
        {
            if (IsValid)
            {
                Manufacturer brand = ManufacturerManager.GetById(BrandId);

                brand.BrandName = tbxBrandName.Text.Trim();
                string tempImageFolder = UploadTemporaryFiles();
                string tmpPartnerBanner = UploadTemporaryBannerFiles();
                if (!String.IsNullOrEmpty(tmpPartnerBanner))
                {
                    brand.BrandBannerImageUrl = tmpPartnerBanner;
                }

                string brandLogo = brand.BrandImageUrl;
                if (!String.IsNullOrEmpty(tempImageFolder))
                {
                    brandLogo = ProductFileAccessUtility.UploadImage(ProductFileAccessUtility._brandLogoPath, tempImageFolder);
                }

                brand.BrandImageUrl = brandLogo;
                ManufacturerManager.Update(brand);

                uccResponseMessage.SetSuccessMessage("Partner updated Successfully");
                uccResponseMessage.Visible = true;
            }
        }
        catch (Exception ex)
        {
            uccResponseMessage.SetFailMessage("Failed to update partner");
            uccResponseMessage.Visible = true;
        }
    }

    protected string UploadTemporaryFiles()
    {
        string tempImageFolder = string.Empty;
        if (fluPartnerImage.HasFile)
        {
            tempImageFolder = SiteUtility.TempFolder + fluPartnerImage.FileName;

            if (File.Exists(MapPath(tempImageFolder)))
                FileAccessUtility.DeleteFile(tempImageFolder);

            string contentType = fluPartnerImage.PostedFile.ContentType.ToLower();
            if (contentType == "image/jpg" || contentType == "image/gif" || contentType == "image/png"
                || contentType == "image/jpeg" || contentType == "image/bmp" || contentType == "image/pjpeg"
                || contentType == "image/x-png")
            {
                fluPartnerImage.SaveAs(MapPath(tempImageFolder));
            }

        }
        return tempImageFolder;
    }

    protected string UploadTemporaryBannerFiles()
    {
        string tempImageFolder = string.Empty;
        if (fluPartnerBannerImage.HasFile)
        {
            tempImageFolder = ProductFileAccessUtility._brandBannerPath + fluPartnerBannerImage.FileName;

            if (File.Exists(MapPath(tempImageFolder)))
                FileAccessUtility.DeleteFile(tempImageFolder);

            string contentType = fluPartnerBannerImage.PostedFile.ContentType.ToLower();
            if (contentType == "image/jpg" || contentType == "image/gif" || contentType == "image/png"
                || contentType == "image/jpeg" || contentType == "image/bmp" || contentType == "image/pjpeg"
                || contentType == "image/x-png")
            {
                fluPartnerBannerImage.SaveAs(MapPath(tempImageFolder));
            }

        }
        return fluPartnerBannerImage.FileName;
    }
}